const router = require('koa-router')();
const DB = require('../../module/db.js');
const tools = require('../../module/tools.js');

router.get('/', async (ctx) => {
  let manageList = await DB.find('adminUser', {});
  await ctx.render('admin/manage/list',{
    list: manageList
  });
});
//跳转添加用户页
router.get('/add', async (ctx) => {
  await ctx.render('admin/manage/add');
});
//退出登录
router.get('/doLoginOut', async (ctx) => {
  ctx.session.userInfo = '';
  ctx.redirect(ctx.state.__HOST__ + '/admin/login');
})
//添加用户操作
router.post('/doAdd', async (ctx) => {
  console.log(ctx.request.body);
  let {username, password, confirmPassword} = ctx.request.body;
  if(username.length == 0 || password.length ==0 || confirmPassword.length == 0){
    await ctx.render('admin/error',{
      message: '添加信息不能为空',
      redirect: ctx.state.__HOST__ + '/admin/manage/add'
    })
  }else{
    if(!/^\w{1,20}/.test(username)){
      await ctx.render('admin/error',{
          message: '用户名不合法',
          redirect: ctx.state.__HOST__ + '/admin/manage/add'
      })
    }else if(password != confirmPassword || password.length < 6){
      await ctx.render('admin/error', {
          message: '密码和确认密码不一致，或者密码长度小于6位',
          redirect: ctx.state.__HOST__ + '/admin/manage/add'
      })
    }else{
      //查询用户名是否存在
      let findResult = await DB.find('adminUser', {'username': username});
      if(findResult.length > 0){
        await ctx.render('admin/error', {
          message: '用户名已存在，请换一个',
          redirect: ctx.state.__HOST__ + '/admin/manage/add'
        })
      }else{
        //增加管理员
        await DB.insert('adminUser', {'username': username, 'password': tools.md5(password), 'status': 1, 'time': ''});
        ctx.redirect(ctx.state.__HOST__ + '/admin/manage');
      }
    }
  }
  
});
//请求编辑用户界面
router.get('/edit', async (ctx) => {
  let userId = ctx.request.query._id;
  let userObj = await DB.find('adminUser', {'_id': DB.getObjectId(userId)});
  await ctx.render('admin/manage/edit', {userObj: userObj[0]});
});
//编辑用户界面操作
router.post('/doEdit', async (ctx) => {
  let {_id, username, password, confirmPassword} = ctx.request.body;
  if(password.length ==0 || confirmPassword.length == 0){
    await ctx.render('admin/error',{
      message: '输入信息不能为空',
      redirect: ctx.state.__HOST__ + '/admin/manage/edit'
    })
  }else{
    if(password != confirmPassword || password.length < 6){
      await ctx.render('admin/error', {
          message: '密码和确认密码不一致，或者密码长度小于6位',
          redirect: ctx.state.__HOST__ + '/admin/manage/edit?_id=' + DB.getObjectId(_id)
      })
    }else{
      //修改管理员
      await DB.update('adminUser', {'_id': DB.getObjectId(_id)}, {'password': tools.md5(password), 'status': 1, 'time': ''});
      ctx.redirect(ctx.state.__HOST__ + '/admin/manage')
    }
  }
});
//删除操作
router.get('/delate', async (ctx) => {
  let delateId = ctx.request.query._id;
  let result  = await DB.delate('adminUser', {'_id': DB.getObjectId(delateId)});
  ctx.redirect(ctx.state.__HOST__ + '/admin/manage');
  if(result.n !== 0){
    console.log('删除成功');
  }
});
module.exports = router.routes();